package io.renren.modules.haiyou.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.modules.haiyou.dao.PictureDao;
import io.renren.modules.haiyou.entity.Picture;

import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * @auther: Freeman Liu
 * @date: 2020/11/2 08:29
 */
public class mapperUtils implements Serializable {

    /**
     * @param entity     实体类 zone,scene......
     * @param pictureDao PictureDao
     * @param <T>
     */
    public static <T> void associatePicturesAndEntity(T entity, PictureDao pictureDao) {
        try {
            Class<?> entityClazz = entity.getClass();

            //获取entityId
            Method idMethod = entityClazz.getDeclaredMethod("getId");
            String entityId = (String) idMethod.invoke(entity);

            Method getPicId = entityClazz.getDeclaredMethod("getPicId");
            String picId = (String) getPicId.invoke(entity);
            if (picId == null) {
                return;
            }
            picId = picId.trim();
            String[] picIds = picId.split(";");

            for (String id : picIds) {
                pictureDao.update(null, new UpdateWrapper<Picture>()
                        .set(StrUtil.lowerFirst(entityClazz.getSimpleName()) + "_id", entityId)
                        .eq("id", id));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @param entities    包含 picId 列的对象的集合
     * @param pictureDao  pictureDao
     * @param <T>
     */
//    public static <T> List<T> getUrlsByEntity(List<T> entities, PictureDao pictureDao) {
//        try {
//            if (entities.size() == 0) {
//                return entities;
//            }
//            Class<?> clazz = entities.get(0).getClass();
//            List<String> urls;
//            Picture picture;
//            Method method;
//            String picId;
//            String[] picIds;
//            for (T entity : entities) {
//                method = clazz.getDeclaredMethod("getPicId");
//                picId = (String) method.invoke(entity);
//                if (picId != null) {
//                    picIds = picId.split(";");
//                    urls = new ArrayList<>();
//                    for (String id : picIds) {
//                        picture = pictureDao.selectById(id);
//                        if (picture != null) {
//                            urls.add(picture.getUrl());
//                        }
//                    }
//                    method = clazz.getDeclaredMethod("setUrls", List.class);
//                    method.invoke(entity, urls);
//                }
//            }
//            return entities;
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("getUrlsByEntity出错!!!");
//        }
//    }
//
//    public static <T> List<T> getUserByEntity(List<T> entities, UserDao userDao) {
//        try {
//            if (entities.size() == 0) {
//                return entities;
//            }
//            Class<?> clazz = entities.get(0).getClass();
//            String userId;
//            User user;
//            Method method;
//            for (T entity : entities) {
//                method = clazz.getDeclaredMethod("getUserId");
//                userId = (String) method.invoke(entity);
//                if (userId != null) {
//                    user = userDao.selectById(userId);
//                    method = clazz.getDeclaredMethod("setUser", User.class);
//                    method.invoke(entity, user);
//                }
//            }
//            return entities;
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("getUserByEntity出错!!!");
//        }
//    }
//
//    public static List<Comment> getUserAndReplyUserByComments(List<Comment> comments, UserDao userDao) {
//        for (Comment comment : comments) {
//            comment.setUser(userDao.selectById(comment.getUserId()));
//            comment.setReplyUser(userDao.selectById(comment.getReplyUserId()));
//        }
//        return comments;
//    }
}
